#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

void Handler(int signo)
{
    std::cout << "get a signo : " << signo << std::endl;
    
    while (true)
    {
        pid_t rid = ::waitpid(-1, nullptr, 0);
        if (rid > 0)
        {
            std::cout << "子进程退出了,回收成功,child id: " << rid << std::endl;
        }
        else if (rid == 0)
        {
            std::cout << "退出的子进程已经被全部回收了" << std::endl;
            break;
        }
        else
        {
            std::cout << "wait error" << std::endl;
            break;
        }
    }
}

int main()
{
    //::signal(SIGCHLD,SIG_IGN);
    //::signal(SIGCHLD, Handler);
    for (int i = 0; i < 10; i++)
    {
        if (fork() == 0)
        {
            std::cout << "创建子进程成功，子进程pid为 ： " << getpid() << std::endl;
            sleep(5);
            exit(0);
        }
    }
    while (true)
    {
        std::cout << "我是父进程,pid : " << getpid() << std::endl;
        sleep(1);
    }
    return 0;
}
